package newTest.test17;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.sql.SQLException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;

import vo.UserVO;

public class testDaoMain {
	public  void addAndGet()throws SQLException{
	
//		DaoFactory daoFactory =  new DaoFactory();
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
//		UserDao userDao = daoFactory.userDao();
		UserDao userDao = context.getBean("useDataSource",UserDao.class);
		
		UserVO user1 = new UserVO("newTest1","1","t1");
		UserVO user2 = new UserVO("newTest2","2","t2");
		
		
		userDao.deleteAll();
		assertThat(userDao.getCount(),is(0));
				
		userDao.add(user1);
		assertThat(userDao.getCount(),is(1));
		assertThat(userDao.get(user1.getId()).getId(),is(user1.getId()));
		
		userDao.add(user2);
		assertThat(userDao.getCount(),is(2));
		assertThat(userDao.get(user2.getId()).getId(),is(user2.getId()));
		
	}  
	@Test(expected=EmptyResultDataAccessException.class)
	public void getUserFailure() throws SQLException{
//		DaoFactory daoFactory =  new DaoFactory();
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
//		UserDao userDao = daoFactory.userDao();
		UserDao userDao = context.getBean("useDataSource",UserDao.class);
		
		userDao.deleteAll();
		assertThat(userDao.getCount(),is(0));
	
		userDao.get("imlpwm_id");
		
		
		
	}
	

}
